﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BankSystem
{
    class Bank
    {
        private List<Account> accounts = new List<Account>();

        public void PrintAccountsList()
        {
            foreach (Account acc in accounts)
            {
                Console.WriteLine("ID: {0}", acc.GetAccountNumber());
            }
        }

        public void OpenNewAccount()
        {
            Random rnd = new Random();
            int defAccNumber = rnd.Next(10000000, 99999999);
            int amount = 0;
            
            Account account = new Account(defAccNumber, amount);
            accounts.Add(account);
            Console.WriteLine("Account created. ID: {0} Money amount: {1}", defAccNumber, amount);
            
        }

        private Account AccountByID(int accNumber)
        {
            foreach (Account account in accounts)
            {
                if (accNumber == account.GetAccountNumber())
                {
                    return account;
                }
            }

            throw new System.InvalidOperationException("Such account does not exist.");
        }

        public void CloseAccount(int accNumber)
        {
            try
            {
                Account acc =AccountByID(accNumber);
                accounts.Remove(acc);
            }
            catch (InvalidOperationException e)
            {
                Console.Write(e.Message);
            }
        }

        public void ViewAccountAmount(int accNumber)
        {
            try
            {
                Account acc = AccountByID(accNumber);
                Console.WriteLine("Money amount: ${0}", acc.GetMoneyAmount());
            }
            catch (InvalidOperationException e)
            {
                Console.Write(e.Message);
            }
        }

        public bool GetMoney(int accNumber, double amount)
        {
            try
            {
                Account acc = AccountByID(accNumber);
                return acc.TakeMoney(amount);
                
            }
            catch (InvalidOperationException e)
            {
                Console.Write(e.Message);
                return false;
            }
        }

        public bool DepositeMoney(int accNumber, double amount)
        {
            try
            {
                Account acc = AccountByID(accNumber);
                return acc.PutMoney(amount);

            }
            catch (InvalidOperationException e)
            {
                Console.Write(e.Message);
                return false;
            }
        }

        public void GetAllMoney(int accNumber)
        {
            try
            {
                Account acc = AccountByID(accNumber);
                Console.WriteLine("Money amount: ${0}", acc.GetMoneyAmount());
                acc.TakeMoney(acc.GetMoneyAmount());
            }
            catch (InvalidOperationException e)
            {
                Console.Write(e.Message);
            }
        }


    }
}
